//
//  ViewController.swift
//  Calculator
//
//  Created by 李朋 on 2017/6/1.
//  Copyright © 2017年 李朋. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet fileprivate weak var display: UILabel!
    
    
    fileprivate var userIsInTheMiddleOfTyping = false
    
    /// 按钮点击事件
    ///
    /// - Parameter sender: 点击的按钮
    @IBAction fileprivate func touchDigit(_ sender: UIButton) {
        guard let digit = sender.currentTitle else {
            return
        }
        
        if userIsInTheMiddleOfTyping {
            let textCurrentlyInDisplay = display.text!
            display.text = textCurrentlyInDisplay + digit
        }else {
            display.text = digit
        }
        userIsInTheMiddleOfTyping = true
    }
    
    fileprivate var displayValue: Double {
        get {
            return Double(display.text!) ?? 0.0
        }
        
        set {
            display.text = String(newValue)
        }
    }
    
    fileprivate var brain = CalculatorBrain()
    
    @IBAction fileprivate func performOperation(_ sender: UIButton) {
        guard let mathematicalSymbol = sender.currentTitle else {
            return
        }
        
        if userIsInTheMiddleOfTyping {
            brain.setOperand(operand: displayValue)
            userIsInTheMiddleOfTyping = false
        }
        
        brain.performOperation(symbol: mathematicalSymbol)
        displayValue = brain.result
        
    }
    
}
















